/*
 * @Author: muge
 * @Date: 2025-07-25 10:03:00
 * @LastEditors: muge
 * @LastEditTime: 2025-11-12 16:29:13
 */
import { fileURLToPath, URL } from 'node:url'
import { createRequire } from 'node:module'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';

const require = createRequire(import.meta.url);
const pxtorem = require('postcss-pxtorem');
// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      resolvers: [VantResolver()],
    }),
    Components({
      resolvers: [VantResolver()],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    host: '0.0.0.0', // 允许外部访问
    port: 5173,
    hmr: {
      overlay: false, // 禁用 HMR 错误覆盖层，避免干扰
    },
    watch: {
      usePolling: false, // 使用原生文件系统监听
    },
  },
  optimizeDeps: {
    include: [
      '@tiptap/vue-3',
      '@tiptap/starter-kit',
      '@tiptap/extension-mention',
      '@tiptap/extension-text',
      '@tiptap/extension-document',
      '@tiptap/suggestion',
      '@floating-ui/dom'
    ],
  },
  css: {
    postcss: {
      plugins: [
        pxtorem({
          rootValue: 75, // 设计稿 750px，基准值 75 (750/10=75)，这样 1rem = 75px，750px = 10rem
          propList: ['*'], // 所有属性都转换
          exclude: /node_modules/i, // 排除 node_modules
          selectorBlackList: ['html', 'body'], // 排除 html 和 body，保持它们的 font-size 为 px
          replace: true,
          mediaQuery: false,
          minPixelValue: 0,
        }),
      ],
    },
  },
})
